package com.tuanzi.loan.web.bean.product.prefilter;

import static com.tuanzi.loan.business.constants.DictionaryCode.*;

import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import javax.annotation.PostConstruct;
import javax.faces.bean.ViewScoped;

import cn.tuanzidai.loan.product.client.vo.LoanProductPrefilterRule;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.primefaces.model.TreeNode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.google.common.collect.Lists;
import com.tuanzi.loan.business.entity.common.CommRegionNew;
import com.tuanzi.loan.business.entity.family.ApiActivity;
import com.tuanzi.loan.business.entity.family.ContentActivity;
import com.tuanzi.loan.business.entity.product.prefilter.LoanProductPrefilter;
import com.tuanzi.loan.business.entity.settlement.CompanyInfo;
import com.tuanzi.loan.business.enums.RegionTreeEnum;
import com.tuanzi.loan.business.service.common.CommRegionNewService;
import com.tuanzi.loan.business.service.product.prefilter.LoanProductPrefilterService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.util.GsonUtils;
import com.tuanzi.loan.web.configuration.JSFInfoBean;

import cn.tuanzidai.loan.product.client.vo.LoanProductPrefilterRule;
import lombok.Data;
import lombok.EqualsAndHashCode;

/**
 * @author Ray 2017/09/21
 */
@Data
@Debug
@Component
@ViewScoped
@EqualsAndHashCode(callSuper = true)
public class LoanProductPrefilterInfoBean extends JSFInfoBean<LoanProductPrefilter> {

	private static final long serialVersionUID = -2152778554108785342L;

	@Autowired
	private LoanProductPrefilterService productPrefilterService;
	@Autowired
	private CommRegionNewService commRegionNewService;

	private List<ContentActivity> contentActivities;
	private List<ApiActivity> apiActivities;
	private TreeNode mobileRegionRoot;
	private TreeNode ipRegionRoot;
	private TreeNode[] selectedMobileRegionNodes;
	private TreeNode[] selectedIpRegionNodes;
	private List<CompanyInfo> companyList;
	private List<String> tongdunIndustryList;

	boolean isEditPage = false;

	@PostConstruct
	private void init() {
		String id = getParamFromRequest("id");
		if (StringUtils.isNotBlank(id)) {
			$ = productPrefilterService.findOne(NumberUtils.toLong(id));
			$.setProductPrefilter(GsonUtils.toObject($.getRule(), LoanProductPrefilterRule.class));
			isEditPage = true;
		} else {
			$ = new LoanProductPrefilter();
			$.setProductPrefilter(new LoanProductPrefilterRule());
			isEditPage = false;
			changeStatusToAdd();
		}

		List<String> mobileCities = Lists.newArrayList();
		if (CollectionUtils.isNotEmpty($.getProductPrefilter().getFilterMobileProvinces())) {
			mobileCities.addAll($.getProductPrefilter().getFilterMobileProvinces());
		}

		if (CollectionUtils.isNotEmpty($.getProductPrefilter().getFilterMobileCities())) {
			mobileCities.addAll($.getProductPrefilter().getFilterMobileCities());
		}

		mobileRegionRoot = commRegionNewService.generateTreeNode(mobileCities.parallelStream().collect(Collectors.joining(",")), RegionTreeEnum.ALL);

		List<String> ipCities = Lists.newArrayList();
		if (CollectionUtils.isNotEmpty($.getProductPrefilter().getFilterIpProvinces())) {
			ipCities.addAll($.getProductPrefilter().getFilterIpProvinces());
		}

		if (CollectionUtils.isNotEmpty($.getProductPrefilter().getFilterIpCities())) {
			ipCities.addAll($.getProductPrefilter().getFilterIpCities());
		}
		ipRegionRoot = commRegionNewService.generateTreeNode(ipCities.parallelStream().collect(Collectors.joining(",")), RegionTreeEnum.ALL);
	}

	public void onSave() {
		$.getProductPrefilter().setFilterIpProvinces(
				Stream.of(selectedIpRegionNodes).map(x -> (CommRegionNew) x.getData()).filter(x -> x.getLevel() == 1).map(CommRegionNew::getName).collect(Collectors.toList()));
		$.getProductPrefilter().setFilterIpCities(
				Stream.of(selectedIpRegionNodes).map(x -> (CommRegionNew) x.getData()).filter(x -> x.getLevel() == 2).map(CommRegionNew::getName).collect(Collectors.toList()));

		$.getProductPrefilter().setFilterMobileProvinces(
				Stream.of(selectedMobileRegionNodes).map(x -> (CommRegionNew) x.getData()).filter(x -> x.getLevel() == 1).map(CommRegionNew::getName).collect(Collectors.toList()));
		$.getProductPrefilter().setFilterMobileCities(
				Stream.of(selectedMobileRegionNodes).map(x -> (CommRegionNew) x.getData()).filter(x -> x.getLevel() == 2).map(CommRegionNew::getName).collect(Collectors.toList()));

		$.setRule(GsonUtils.toJson($.getProductPrefilter()));
		productPrefilterService.saveOrUpdate($);
		changeStatusToView();
		addSuccessfullyMessage();
	}

	@Override
	protected List<String> getDictionaryCodes() {
		return Lists.newArrayList(FIRST_PARTY_LOAN_TYPE, APPROVAL_TYPE, CREDIT_CARD_TAG, IDENTITY_TAG, LOAN_NECESSARY_DATA, PROMOTION_TYPE, RATE_TYPE, UNION_INFO, UNION_SERIAL,
				API_SERIAL, PLATFORM, THIRD_PARTY_LONG_TAG, LOAN_PRODUCT_REPAY_TYPE, LOAN_PRODUCT_TYPE, BLACK_LIST_TYPE);
	}

}
